jQuery(document).ready(function($) {
	var SCROLL_SPEED = 500;
	$("#s").watermark("Cauta sfaturi despre?", { useNative: false });
	
	// initialize fancybox for article images
	// first, check if fancybox exists, otherwise will crash
	$("a.fancybox").length > 0 ? $("a.fancybox").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'speedIn'		: 300, 
		'speedOut'		: 300,
		'overlayShow'	: true,
		'overlayOpacity': 0.3,
		'overlayColor'	: '#666',
		onStart			: function(selectedArray, selectedIndex, selectedOpts){
							$(selectedArray).animate({opacity: 0}, 100, function(){
								// oncomple animation set visibility hidden (needed for IE)
								$(this).css('visibility', 'hidden');
							})
						},
		onClosed		: function(selectedArray, selectedIndex, selectedOpts){
							// visibility is needed for IE
							$(selectedArray).css('opacity', '1').css( 'visibility', 'visible');
						}
		
	}) : {};

	$("#searchform").submit(function(event){
		var $s = $("#s");
		if($.trim($s.val()).length <= 0){
			event.preventDefault();
			$s.focus();
			return false;
		}
	});
	
	$(".scrollTop").click(function(event){
		event.preventDefault();
		$('html, body').animate({scrollTop: 0}, SCROLL_SPEED);
	});
	
	// Product characteristic
	$('.mainTopCharacteristic a').click(function(event){
		event.preventDefault();
		var element = $(this).attr("href").replace("#", "");
		var scrollTopVal = $("a[name="+element+"]").offset().top;
		$('html, body').animate({scrollTop: scrollTopVal}, SCROLL_SPEED);
		window.location = $(this).attr("href");
	});
	
	$(".meta-prep-entry-comments, .entry-comments").click(function(){
		var scrollTopVal = $("#comments").offset().top - 5;
		$('html, body').animate({scrollTop: scrollTopVal}, SCROLL_SPEED);
	});
	
	/* Alerts functions */	
	var ALERT_COOKIE_NAME = "ESFATURICOOKIEALERT";
	if (!GetCookie(ALERT_COOKIE_NAME)){
		$("#alerts").show();
	}
	
	$("#alert-close").click(function(){
		$("#alerts").hide();
		SetCookie(ALERT_COOKIE_NAME, false, 30);
	});
	
	// Cookies handle functions
	function SetCookie(c_name, value, exdays){
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value = escape(value) + ((exdays==null) ? "" : "; expires=" + exdate.toUTCString());
		document.cookie = c_name + "=" + c_value;
	}
	
	function GetCookie(c_name){
		var i,name,value,ARRcookies = document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++){
			name = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
			name = name.replace(/^\s+|\s+$/g, "");
			if (name == c_name){
				value = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
				return unescape(value);
			}
		}
		return null;
	}
	
	/* Categories widget functions */
	//var SELECTED_CAT_COOKIE_NAME = "ESFATURISELECTEDCATEGORYCOOKIE";
	//var selectedCatId = GetCookie(SELECTED_CAT_COOKIE_NAME);
	//if (selectedCatId != null){
	//	if (selectedCatId.indexOf('main-') > -1){ // main category selected
	//		var id = selectedCatId.split("-")[1];
	//		var childrenList = $("#categories-tree").find("a#cat-"+id).parent().children("ul");
	//		ToggleCategory(childrenList, false);
	//	}
	//	else{  subcategory selected
	//		var selectedElement = $("#categories-tree").find("a#cat-"+selectedCatId);
	//		$(selectedElement).addClass('selected');
	//		var childrenList = $(selectedElement).parent().parent();
	//		ToggleCategory(childrenList, false);
	//	}
	//}
	
	//$("#categories-tree>li>img").click(function(){
	//	var childrenList = $(this).parent().children("ul");		
	//	ToggleCategory(childrenList, true);
	//});
	
	//$("#categories-tree>li>a").click(function(){
	//	$("#categories-tree").find("a.selected").removeClass("selected");
	//	var selectedId = $(this).attr('id').split("-")[1];
	//	SetCookie(SELECTED_CAT_COOKIE_NAME, "main-"+selectedId, 30);
	//	var childrenList = $(this).parent().children("ul");
	//	if ($(childrenList).is(":visible") == false)
	//		ToggleCategory(childrenList, true);
	//});
	
	//$("#categories-tree>li>ul>li>a").click(function(){
	//	$("#categories-tree").find("a.selected").removeClass("selected");
	//	$(this).addClass("selected");
	//	var selectedId = $(this).attr('id').split("-")[1];
	//	SetCookie(SELECTED_CAT_COOKIE_NAME, selectedId, 30);
	//});
	
	//function ToggleCategory(childrenList, animate){
	//	var img = $(childrenList).parent().children("img");
	//	var imageSrc = $(img).attr("src");
	//	var currentImageName = imageSrc.replace(/^.*\/|\.[^.]*$/g, ''); // get image name
	//	var isExpanded = $(childrenList).is(":visible");
	//	
	//	if (!isExpanded){
	//		$(childrenList).show(animate ? 300 : "");
	//		$(img).attr("src", imageSrc.replace(currentImageName, "category-minus"));
	//	}
	//	else{
	//		$(childrenList).hide(animate ? 300 : "");
	//		$(img).attr("src", imageSrc.replace(currentImageName, "category-plus"));
	//	}
	//}
	
	/* Share tools functions */
	$("div.shareTools a.share-email").fancybox({
		'scrolling' : 'no',
		'titleShow' : false,
		'onClosed' : function() {
			$("#sbeTxtEmailAddress").removeClass("textbox-error");
			$("#sbeTxtName").removeClass("textbox-error");
		}
	});
	
	$("#sbeBtnSendByEmail").click(function() {
		var postUrl = $("#hdnPostUrl").val();
		var email = $.trim($("#sbeTxtEmailAddress").val());
		var name = $.trim($("#sbeTxtName").val());
		var isValid = ValidateName(name) && ValidateEmail(email);
	
		if (isValid){
			$.ajax({
				type: "POST",
				url: $("#hdnTempDirectory").val() + '/ajax-calls.php',
				data: "action=sendByEmail&email="+EncodeUrl(email)+"&name="+EncodeUrl(name)+"&postUrl="+EncodeUrl(postUrl),
				beforeSend: function(xhr) {
					$("#sbeImgLoader").show();
				},
				success: function(data) {
					$("#sbeImgLoader").hide();
					$.fancybox(data);
				},
				error: function() {
					$("#sbeImgLoader").hide();
				}
			});
		}
	});
	
	$("#send-by-email-form p input[type='text']").blur(function(){
		var email = $.trim($("#sbeTxtEmailAddress").val());
		var name = $.trim($("#sbeTxtName").val());
		ValidateEmail(email); ValidateName(name);
	});
	
	function ValidateEmail(email){
		email = $.trim(email);
		var regex =  /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var isValid = regex.test(email);
		if (isValid){
			$("#sbeTxtEmailAddress").removeClass("textbox-error");
		}
		else{
			$("#sbeTxtEmailAddress").addClass("textbox-error");
		}
		return isValid;
	}
	
	function ValidateName(name){
		var isValid = $.trim(name).length > 1;
		if (isValid){
			$("#sbeTxtName").removeClass("textbox-error");
		}
		else{
			$("#sbeTxtName").addClass("textbox-error");
		}
		return isValid;
	}
	
	function EncodeUrl(url){
		return encodeURIComponent(url);
	}
	
	$(".share-print").click(function(event){
		event.preventDefault();
		var href = $(this).attr("href");
		window.open(href, '', 'width=650,height=450,left=150,top=150,scrollbars=yes');
	});
});